package com.qiyou.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qiyou.dao.IBlogCommentDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.BlogComment;

/**
 * 博客评论
 * 
 * @author 赛艳志
 */
@WebServlet("/BlogReply")
public class BlogReplyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//	    doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        
        long blogID = Long.parseLong(request.getParameter("blogID"));
        long bcID = request.getParameter("bcID") == null ? -1 : Long.parseLong(request.getParameter("bcID"));
        String userID = request.getParameter("userID");
        String replycontent = request.getParameter("replycontent");
        
        BlogComment comment = new BlogComment();
        
        comment.setBlogID(blogID);
        comment.setMotherID(bcID);
        comment.setUserID(userID);
        comment.setContent(replycontent);
        
        IBlogCommentDao commentDao = DaoFactory.getBlogCommentDao();
        boolean flag = false;
        try {
            flag = commentDao.doCreate(comment);
        } catch (SQLException e) {
        }
        
        if (flag) {
            response.getWriter().print("<script language='javascript'>alert('评论成功');</script>");
        }else {
            response.getWriter().print("<script language='javascript'>alert('评论失败');</script>");
        }
        
        request.getRequestDispatcher("BlogInfo?id=" + blogID).forward(request, response);
	}

}
